//
//  ProgressViewController.m
//  qq
//
//  Created by 王成龙 on 2016/11/17.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "ProgressViewController.h"

@interface ProgressViewController ()
@property (weak, nonatomic) IBOutlet UIButton *xiazai;
@property (weak, nonatomic) IBOutlet UIProgressView *imgPro;

@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@end

@implementation ProgressViewController

- (IBAction)onStartDownload:(id)sender {
    NSURL *url = [NSURL URLWithString:@"http://www.cxdq.com/d/file/2011/10-21/2895599b26c396e46fc4892eebc2a654.jpg"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    NSURLSessionDataTask *task = [session dataTaskWithURL:url];
    [task resume];
    self.imgPro.progress = 0;
    self.imgView.image = nil;
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
{
    self.imgView.image = [UIImage imageWithData:imgData];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    [self.imgPro setProgress:(float)dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive animated:YES];
    if (!imgData) {
        imgData = [NSMutableData new];
    }
    [imgData appendData:data];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
